package com.fr.report.script.function;

import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;

public class COMBIN extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    if (paramArrayOfObject.length < 2)
      return Primitive.ERROR_NAME;
    Object localObject1 = paramArrayOfObject[0];
    Object localObject2 = paramArrayOfObject[1];
    if ((localObject1 instanceof Number) && (localObject2 instanceof Number))
    {
      double d1 = ((Number)localObject1).doubleValue();
      int i = ((Number)localObject1).intValue();
      double d2 = ((Number)localObject2).doubleValue();
      int j = ((Number)localObject2).intValue();
      if ((i < j) || (i < 0) || (j < 0) || (d1 - i != 0.0D) || (d2 - j != 0.0D))
        return Primitive.ERROR_VALUE;
      return new Long(jieCheng(i) / jieCheng(j) * jieCheng(i - j));
    }
    return Primitive.ERROR_NAME;
  }

  private long jieCheng(int paramInt)
  {
    long l = 1L;
    for (int i = 1; i <= paramInt; ++i)
      l *= i;
    return l;
  }

  public Function.Type getType()
  {
    return Function.MATH;
  }

  public String getCN()
  {
    return "COMBIN(number,number_chosen): 返回若干个指定对象的组合数。该函数与数学表达式为Cnk功能相同。\nNumber或数学表达式中的“n”指对象总数。\nNumber_chosen或数学表达式中的“k”指在对象总数中某一组合的数量。\n备注:\n    Number必须是正整数，number_chosen必须是非负整数。\n    如果number和number_chosen小于0或number小于number_chosen，函数返回错误信息*NUM!。\n    对象组合是对象的子集。与排列不同的是，组合不涉及对象内部的先后顺序，而顺序对排列是非常重要的。\n    假设number=n，number_chosen=k，则: COMBIN(n,k)=Cnk=n!/(k!(n-k)!)。\n示例:\nCOMBIN(5,6)等于*NUM!。\nCOMBIN(5,2)等于10。";
  }

  public String getEN()
  {
    return "COMBIN(number,number_chosen): Returns the number of combinations for a given number of items, functionally equals Cnk.\nNumber is the number of items.\nNumber chosen is the number of items in each combination.\n\nRemarks:\n1. Number is truncated to plus integers, number_chosen is truncated to integers exclude negative.\n2. If number < 0, number_chosen < 0, or number < number_chosen, COMBIN returns the #NUM! error value. \n3. A combination is any set or subset of items, regardless of their internal order. Combinations are distinct from permutations, for which the internal order is significant. \n    Suppose number=n, number_chosen=k, then COMBIN(n,k)=Cnk=n!/(k!(n-k)!).\n\nExample:\n   COMBIN(5,6)=*NUM!\n   COMBIN(5,2)=10";
  }
}